

-- Namespace
	local _,mod = ...


-- Class color
	local RAID_CLASS_COLORS = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS
	local _,enclass = UnitClass('player')
	local cc = RAID_CLASS_COLORS[enclass] or { r = 1, g = 1, b = 1 }


-- Constants
	local font = {mod.path..'media\\bavaria.ttf',8,'OUTLINEMONOCHROME'}
	
	local c_bar = {cc.r*.75,cc.g*.75,cc.b*.75}
	local c_barbg =  {.33,.33,.33}
	local c_border = {.07,.07,.07}


-- Cooldown button
	function mod.newbutton()
		local btn = mod:CreateFrame('Frame',nil,UIParent)
			btn:SetHeight(18) btn:SetWidth(21)
			btn:EnableMouse(true)
			btn:SetScript('OnEnter',function(self)
				GameTooltip:SetOwner(self,'ANCHOR_CURSOR')
				GameTooltip:SetHyperlink(self.link)
			end)
			btn:SetScript('OnLeave',function(self)
				GameTooltip:ClearLines()
				GameTooltip:Hide()
			end)
			btn:Listen('cooldown',function(self,spell,_,_,link)
				self.spell = spell
				self.link = link
			end)
			btn:Listen('time',function(self,exp)
				self.exp = exp
			end)
			btn:Listen('order',function(self,id)
				self:ClearAllPoints()
				self:SetPoint('TOPLEFT',UIParent,'CENTER',-238+(((id-1)%8)*29),-198-(floor((id-1)/8)*26))
			end)
		
		local bg = btn:CreateTexture(nil,'BACKGROUND')
			bg:SetAllPoints(true)
			bg:SetTexture(unpack(c_border))
			btn.bg = bg
		
		local icon = btn:CreateTexture(nil,'ARTWORK')
			icon:SetHeight(16) icon:SetWidth(16)
			icon:SetPoint('LEFT',1,0)
			icon:SetTexCoord(.1,.9,.1,.9)
			icon:Listen('cooldown',function(self,_,icon,type)
				self:SetTexture(icon)
				self:SetDesaturated((type == 'pet'))
			end)
			btn.icon = icon
		
		local timebar = btn:CreateTexture(nil,'ARTWORK')
			timebar:SetHeight(16) timebar:SetWidth(2)
			timebar:SetPoint('BOTTOMRIGHT',-1,1)
			timebar:SetTexture(unpack(c_bar))
			timebar.anim = function(elapsed)
				local self = timebar
				local min,max = self.min,self.max
				min = min + elapsed; self.min = min
				local left = max - min
				self:SetHeight((left/max)*16)
			end
			timebar:Listen('time',function(self,exp,duration)
				if exp then
					self.min = GetTime() - (exp - duration)
					self.max = duration
					mod:anim_register(self.anim)(0)
				else
					self.min = nil
					self.max = nil
					self:SetHeight(16)
					mod:anim_unregister(self.anim)
				end
			end)
			btn.timebar = timebar
		
		local timebarbg = btn:CreateTexture(nil,'BORDER')
			timebarbg:SetHeight(16) timebarbg:SetWidth(2)
			timebarbg:SetPoint('BOTTOMRIGHT',-1,1)
			timebarbg:SetTexture(unpack(c_barbg))
			btn.timebarbg = timebarbg
		
		local timetext = btn:CreateFontString(nil,'OVERLAY')
			timetext:SetPoint('BOTTOMLEFT',-2,-2)
			timetext:SetFont(unpack(font))
			timetext:SetJustifyH('LEFT')
			timetext.timer = function(elapsed)
				local self = timetext
				local min,max = self.min,self.max
				min = min + elapsed; self.min = min
				local left = max - min
				if left > 60 or left < 0 then
					self:SetText('')
				elseif left > 10 then
					self:SetText(('%d'):format(left))
				else
					self:SetText(('%.1f'):format(left))
				end
			end
			timetext:Listen('time',function(self,exp,duration)
				if exp then
					self.min = GetTime() - (exp - duration)
					self.max = duration
					mod:timer_register(.1,true,self.timer)(0)
				else
					self.min = nil
					self.max = nil
					self:SetText('')
					mod:timer_unregister(self.timer)
				end
			end)
			btn.timetext = timetext
		
		return btn
	end


-- Pulse
	local pulse = mod:CreateFrame('Frame',nil,UIParent)
		pulse:SetHeight(24) pulse:SetWidth(24)
		pulse:SetFrameLevel(3)
		pulse:SetPoint('CENTER',0,-140)
		pulse:Hide()
		pulse.anim = function(elapsed)
			local self = pulse
			local min,max = self.min,self.max
			min = min + elapsed; self.min = min
			local perc = min / max
			if perc >= 1 then
				self:Hide()
				self.min = nil
				self.max = nil
				mod:anim_unregister(self.anim)
			elseif perc > .8 then
				self:SetAlpha(1-((perc-.8)*5))
			end
		end
		pulse:Listen('pulse',function(self,icon)
			if icon then
				self:Show()
				self:SetAlpha(1)
				self.min = 0
				self.max = 1
				mod:anim_register(self.anim)(0)
			end
		end)
		mod.pulse = pulse
	
	local icon = pulse:CreateTexture(nil,'ARTWORK')
		icon:SetPoint('TOPLEFT',1,-1) icon:SetPoint('BOTTOMRIGHT',-1,1)
		icon:SetTexCoord(.1,.9,.1,.9)
		icon:Listen('pulse',function(self,spell,icon,type,link)
			self:SetTexture(icon)
			self:SetDesaturated((type == 'pet'))
		end)
		pulse.icon = icon
	
	local border = pulse:CreateTexture(nil,'BACKGROUND')
		border:SetAllPoints(true)
		border:SetTexture(unpack(c_border))
		pulse.border = border
